<template>
  <div class="drawer_boxs" >
    <!-- <div style="display: flex; width: 100%; justify-content: flex-end;">
        <el-select
          v-model="selectValue"
          placeholder="监区"
          style="width: 200px"
        >
          <el-option
            v-for="item in optionsS"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-button
          title="查询"
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          :icon="Search"
          @click="getExceptionReportSelect"
        >
        </el-button>
      </div> -->

  <div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClickTab">
      <el-tab-pane label="未处理" name="wcl"></el-tab-pane>
      <el-tab-pane label="已处理" name="ycl"></el-tab-pane>
     
    </el-tabs>
  </div>

    <div class="box_wcl isTable" v-if="activeName == 'wcl'">
      <el-table
        :data="ycqkList"
        fit
        highlight-current-row
        stripe
         :span-method="mergeCells"
        style="width: 100%"
        border
      height="680"
      >
      <el-table-column prop="zcfr" label="部门" align="center" width="120">
          <template #default="scope">
            {{ scope.row.orgName }}
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="时间" align="center" width="140">
          <template #default="scope">
            <div>
              {{
                scope.row.createDate.split("T")[0] +
                " " +
                scope.row.createDate.split("T")[1].split(":")[0] +
                ":" +
                scope.row.createDate.split("T")[1].split(":")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="zcfr" label="上报人" align="center" width="120">
          <template #default="scope">
            {{ scope.row.reportUserName }}
          </template>
        </el-table-column>

        <el-table-column prop="zcfr" label="类型" align="center" width="200">
          <template #default="scope">
            {{ scope.row.exceptType }}
          </template>
        </el-table-column>

        <el-table-column prop="ld" label="状态" align="center" width="80">
          <template #default="scope">
     
            <div style="color: greenyellow " v-if="scope.row.state">
              已处理
            </div>
            <div style="color: red;" v-else>
              未处理
            </div>
            <!-- <el-button v-else text size="small" type="danger">未处理</el-button> -->
          </template>
        </el-table-column>
        <el-table-column  prop="ld" label="情况描述" align="center">
          <template #default="scope">
            {{ scope.row.content }}
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="box_wcl isTable" v-if="activeName == 'ycl'">
      <el-table
        :data="ycqkList"
        fit
        highlight-current-row
        stripe
         :span-method="mergeCells"
        style="width: 100%"
        border
          height="680"
      >
          <el-table-column prop="zcfr" label="部门" align="center" width="120">
          <template #default="scope">
            {{ scope.row.orgName }}
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="时间" align="center"  width="160">
          <template #default="scope">
            <div>
              {{
                scope.row.createDate.split("T")[0] +
                " " +
                scope.row.createDate.split("T")[1].split(":")[0] +
                ":" +
                scope.row.createDate.split("T")[1].split(":")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="zcfr" label="处理人" align="center" width="180">
          <template #default="scope">
            {{ scope.row.handleUserName }}
          </template>
        </el-table-column>

        <el-table-column prop="zcfr" label="类型" align="center" width="200">
          <template #default="scope">
            {{ scope.row.exceptType }}
          </template>
        </el-table-column>

        <el-table-column prop="ld" label="状态" align="center" width="80">
          <template #default="scope">
            <div style="color: green " v-if="scope.row.state">
              已处理
            </div>
            <div style="color: red;" v-else>
              未处理
            </div>
          </template>
        </el-table-column>
        <el-table-column  prop="ld" label="处理信息" align="center">
          <template #default="scope">
            {{ scope.row.handleContent }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-if="activeName == 'sb'">
      <div style="display: flex; justify-content: space-between">
        <div style="display: flex; margin-bottom: 10px">
          <div style="width: 67px" class="flex_center_center">类型:</div>
          <div v-if="!stateYc">{{ ycqkObj.exceptType }}</div>
          <el-select
            v-else
            :disabled="!stateYc"
            v-model="ycqkObj.exceptType"
            size="large"
            style="width: 260px"
            placeholder=" "
          >
            <el-option
              v-for="item in optionsType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div style="display: flex; margin-bottom: 10px">
          <div style="width: 67px" class="flex_center_center">上报人:</div>
          <div v-if="!stateYc">
            {{ ycqkObj.reportUserName }}
          </div>
          <el-select
            v-else
            :disabled="!stateYc"
            v-model="ycqkObj.reportUser"
            size="large"
            placeholder=" "
            style="width: 260px"
          >
            <el-option
              v-for="item in optionsSbr"
              :key="item.account"
              :label="item.name"
              :value="item.account"
            />
          </el-select>
        </div>
      </div>
      <div style="display: flex; margin-bottom: 10px">
        <div style="min-width: 70px" class="flex_center_center">情况:</div>
        <div
          v-if="!stateYc"
          style="height: 136px; display: flex; align-items: center"
        >
          {{ ycqkObj.content }}
        </div>
        <el-input
          v-else
          :disabled="!stateYc"
          v-model="ycqkObj.content"
          :rows="6"
          type="textarea"
          placeholder="情况说明"
        />
      </div>

      <div v-if="!stateYc">
        <el-divider content-position="left"> 处理 </el-divider>
        <div style="display: flex; margin-bottom: 10px">
          <div style="width: 67px" class="flex_center_center">处理人:</div>
          <el-select
            :disabled="ycqkObj.state"
            v-model="ycqkObj.handleUser"
            size="large"
            placeholder=" "
            style="width: 260px"
          >
            <el-option
              v-for="item in optionsClr"
              :key="item.account"
              :label="item.name"
              :value="item.account"
            />
          </el-select>
        </div>
        <div style="display: flex; margin-bottom: 10px">
          <div style="width: 78px" class="flex_center_center">处理:</div>
          <el-input
            v-model="ycqkObj.handleContent"
            :rows="6"
            :disabled="ycqkObj.state"
            type="textarea"
            placeholder="处理结果"
          />
        </div>
      </div>
    </div>

    <div class="dialog-footer" v-if="activeName == 'sb'">
      <el-button type="primary" @click="ycsbSave"> 保存 </el-button>
    </div>
  </div>
</template>

<script setup>
import { ElMessage,  } from "element-plus";
import http from "@/utils/api";
import {
    Search,
    Share,
    Upload,
    TakeawayBox,
    Refresh,
    Collection,
    Plus,
    Loading,
    FolderDelete,
    Back,
  } from "@element-plus/icons-vue";
import {
  ref,
  watch,
  onMounted,
  onBeforeUnmount,
  reactive,
  onBeforeMount,
  nextTick,
  onUnmounted,
  computed
} from "vue";
import { defineProps, defineEmits } from "vue";
let activeName = ref("wcl");
const props = defineProps({
  selectValue: {
    type: Number,
    default: 44,
  },
  orgIds: {
    type: String,
    default: '44',
  },
  jqArr: {
    type: Array,
    default: [],
  },
});


const optionsS = ref([
  {
    id: "Option1",
    name: "Option1",
  },
]);
let daydata = ref("");
let ycqkObj = ref({});
// 异常状态
let stateYc = ref(true);
const optionsType = [
  {
    value: "罪犯改造活动",
    label: "罪犯改造活动",
  },
  {
    value: "罪犯异常",
    label: "罪犯异常",
  },
  {
    value: "罪犯违规违纪",
    label: "罪犯违规违纪",
  },
  {
    value: "罪犯提讯、提审",
    label: "罪犯提讯、提审",
  },
  {
    value: "罪犯疾病处置",
    label: "罪犯疾病处置",
  },
];
watch(() => props.isJudge, (newValue, oldValue) => {
  console.log('isJudge changed from', oldValue, 'to', newValue);

    activeName.value = "wcl";
    stateYc.value = true;
  
}, {
  deep: true, // 深度监听
  immediate: true // 立即触发回调
});



const emit = defineEmits(["childEvent"]);
let dateSfm = ref("");
let hour = ref("");

const ycTableClick = (row) => {
  ycqkObj.value = row;
  activeName.value = "sb";
  stateYc.value = false;
};

// 获取年月日 时
const getFormattedDateTime = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  dateSfm.value = `${hours}:${minutes}:${seconds}`;
  hour.value = String(now.getHours()).padStart(2, "0");
  daydata.value = `${year}-${month}-${day}`;

  return `${year}-${month}-${day} ${hour.value}`;
};
let isIndex = ref('0')
const handleClickTab = (tab) => {
  isIndex.value = tab.index;
  getExceptionReportSelect(tab.index);
};
let ycqkList = ref([]);
const getExceptionReportSelect = async () => {
  let obj = {
    page: 1,
    limit: 299,
    orgId: sessionStorage.getItem("orgIds"),
    exceptType: "",
    startDate: "",
    endDate: "",
    state:  isIndex.value,
    "reportType": "zf,jg",
    reportState: "1",
  };
  let res = await http.znjw.ExceptionReportSelect(obj);
  if (!res) return;
  if (res.code == 0 ) {
    ycqkList.value = res.data.data;
    ycqkList.value = sortedArray.value;
    if( isIndex.value == 1){
      ycqkList.value = res.data.data;
      ycqkList.value =  sortedArray.value;
    }
  }
};

// 使用计算属性来排序数组
const sortedArray = computed(() => {
  return [...ycqkList.value].sort((a, b) => a.orgName.localeCompare(b.orgName));
});

const mergeCells = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    // 只合并第一列
    const prevRow = ycqkList.value[rowIndex - 1];
    const currRow = ycqkList.value[rowIndex];
    if (prevRow && prevRow.orgName === currRow.orgName) {
      return [1, 0]; // [rowspan, colspan]
    } else {
      const count = 1;
      let mergeCount = 1;
      for (let i = rowIndex + 1; i < ycqkList.value.length; i++) {
        if (ycqkList.value[i].orgName === currRow.orgName) {
          mergeCount++;
        } else {
          break;
        }
      }
      return [mergeCount, 1];
    }
  } else {
    return [1, 1];
  }
};


//异常上报提交
const ycsbSave = async () => {
  let obj = {
    orgId: props.orgIds,
    state: 0,
    reportType:'exce'
    // file: "",
  };
  if (!ycqkObj.value.content || !ycqkObj.value.reportUser) {
    ElMessage({
      type: "error",
      message: "请完善异常信息",
    });
    return;
  }
  let res;
  if (ycqkObj.value.handleContent) {
    (obj["handleContent"] = ycqkObj.value.handleContent || ""),
      (obj["handleDate"] = new Date().toLocaleString().replace(/\//g, "-")),
      (obj["handleUser"] = ycqkObj.value.handleUser || ""),
      (obj["state"] = 1),
      (obj["id"] = ycqkObj.value.id),
      (res = await http.base.ExceptionReportUpdate(obj));
  } else {
    (obj["content"] = ycqkObj.value.content || ""),
      (obj["reportUser"] = ycqkObj.value.reportUser || ""),
      (obj["exceptType"] = ycqkObj.value.exceptType || ""),
      (obj["reportDate"] =
        ycqkObj.value.reportDate ||
        new Date().toLocaleString().replace(/\//g, "-")),
      (res = await http.base.ExceptionReportAdd(obj));
  }
  if (res.code == 0) {
    ElMessage({
      message: "上传成功",
      type: "success",
      plain: true,
    });
    ycqkObj.value = {}
     
    emit("childEvent", 'false');
  }
};

watch(
  () => props.selectValue,
  (newValue, oldValue) => {
    if (newValue) {
      if (props.jqArr) {
        optionsS.value = props.jqArr;
      }
      getFormattedDateTime();
      getExceptionReportSelect();
    } else {
    }
  },
  {
    deep: true, // 深度监听
    immediate: true, // 立即触发回调
  }
);

</script>

<style scoped lang="scss">
.box_wcl{
  overflow-y: scroll;
}
::v-deep .isTable .el-table {
  background-color: rgb(8, 32, 78) !important;
  .el-table tr {
    background-color: rgb(8, 32, 78) !important;
  }
  .cell {
    height: auto;
    color: #fff;
    text-align: center;
    // border: 1px solid #ccc;
    background: rgb(8, 32, 78) !important;
    font-size: 12px;
    line-height: 26px;
  }
  .el-table__cell {
    border-color: 1px solid rgb(5, 112, 146);
    padding: 2px 0px !important;
    background: rgb(8, 32, 78) !important;
  }
  .el-table--fit .el-table__inner-wrapper:before {
    display: none !important;
  }
  .el-table th.el-table__cell.is-leaf {
    border: none !important;
  }
  --el-table-border-color: rgb(5, 112, 146);
}
.drawer_boxs {
  width: 100%;
  height: 100%;
  padding: 10px;
 
}
</style>
